package com.htudata.core.util.operation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class FastJsonOperationUtils {

  public static JSONObject JSONArrayToJSONObejct(JSONArray jsonArray, String keyField, boolean keepLast) {
    JSONObject result = new JSONObject();
    JSONObject value = null;
    for (Object obj : jsonArray) {
      value = (JSONObject)obj;
      if (!keepLast && 
        result.containsKey(value.getString(keyField)))
        continue; 
      result.put(value.getString(keyField), value);
    } 
    return result;
  }
  
  public static String objToJson(Object obj) {
    String result = JSON.toJSONString(obj);
    return result;
  }
  
  public static <T> T jsonToObject(String json, Class<T> cls) {
    T result = (T)JSON.parseObject(json, cls);
    return result;
  }
  
  public static <T> T jsonToObject(String json) {
    T result = (T)JSON.parseObject(json, new TypeReference<T>() {
        
        },  new com.alibaba.fastjson.parser.Feature[0]);
    return result;
  }
  
  public static JSONArray jsonToJSONArray(String json) {
    JSONArray result = JSONArray.parseArray(json);
    return result;
  }
  
  public static JSONObject jsonToJSONObject(String json) {
    JSONObject result = JSONArray.parseObject(json);
    return result;
  }
  
  public static void sortByIntField(JSONArray jsonArray, final String sortField, final String sortType) {
    Collections.sort((List<?>)jsonArray, new Comparator() {
          public int compare(Object obj1, Object obj2) {
            JSONObject jsonObject1 = (JSONObject)obj1;
            JSONObject jsonObject2 = (JSONObject)obj2;
            Integer value1 = Integer.valueOf(jsonObject1.getIntValue(sortField));
            Integer value2 = Integer.valueOf(jsonObject2.getIntValue(sortField));
            int result = 0;
            if ("asc".equals(sortType)) {
              result = value1.compareTo(value2);
              return result;
            } 
            if ("desc".equals(sortType)) {
              result = value2.compareTo(value1);
              return result;
            } 
            return result;
          }
        });
  }
  
  public static void sortByLongField(JSONArray jsonArray, final String sortField, final String sortType) {
    Collections.sort((List<?>)jsonArray, new Comparator() {
          public int compare(Object obj1, Object obj2) {
            JSONObject jsonObject1 = (JSONObject)obj1;
            JSONObject jsonObject2 = (JSONObject)obj2;
            Long value1 = Long.valueOf(jsonObject1.getLongValue(sortField));
            Long value2 = Long.valueOf(jsonObject2.getLongValue(sortField));
            int result = 0;
            if ("asc".equals(sortType)) {
              result = value1.compareTo(value2);
              return result;
            } 
            if ("desc".equals(sortType)) {
              result = value2.compareTo(value1);
              return result;
            } 
            return result;
          }
        });
  }
}
